/**
 * create by gby 20180902
 */
$(function () {
    obj.init();
});

var obj = new function () {
    'use strict';
    return {
        dataGrid: null,
        params: {
            ids: null
        },
        init: function () {
            this.dataGrid = new xhDataGrid($('#List'), 'Server/getMicroServerData');
            this.dataGrid.parseToolbar().init();
            xhTool.bindDocKeyDown($('#bt_list_search'));
            this.params.ids = this.dataGrid.getSelectValues('serverId');
            this.form = new xhForm($('#xh_form'));
            this.form.parse().bindRequired($('#bt_form_add,#bt_form_edit'));
            this.listener();
        },
        listener: function () {
            var $this = this;
            //有修改权限，双击打开表单
            if ($('#bt_form_edit').length > 0) {
                $this.dataGrid.onDblClickRow = function (rowIndex, rowData) {
                    $this.form.reset().fill(rowData);
                    if ($(" input[ name='domainOrIp' ] ").val()) {
                        $("#domainOrIp").css("border-color","#e4e4e4");
                    }
                    if ($(" input[ name='serverType' ] ").val()) {
                        $("#serverType").css("border-color","#e4e4e4");
                    }
                    $("#classIds").val(rowData.classIdList);
                    $("#schList").val(rowData.schList).trigger('change');
                    xhExtra.forwardToForm(true);
                };
            }
            $("#domainOrIp").blur(function() {
                var obj = this.value;
                if (obj) {
                    $("#domainOrIp").css("border-color","#e4e4e4");
                } else {
                    $("#domainOrIp").css("border-color","#edbc6c");
                }
            });
            $("#serverType").blur(function() {
                var obj = this.value;
                if (obj) {
                    $("#serverType").css("border-color","#e4e4e4");
                } else {
                    $("#serverType").css("border-color","#edbc6c");
                }
            });
            xhTool.bindDocKeyDown(
                // 绑定搜索事件
                $('#bt_list_search').click(function () {
                    $this.funObj().search();
                })
            );
            // 返回
            $('#bt_form_back').click(function () {
                $.overtFun.clickBackForm();
            });
            // 重置
            $('#bt_form_reset').click(function () {
                $("#domainOrIp").css("border-color","#edbc6c");
                $("#serverType").css("border-color","#edbc6c");
                $.overtFun.clickResetForm();
            });
            // 新增
            $('#bt_list_add').click(function () {
                $("#domainOrIp").css("border-color","#edbc6c");
                $("#serverType").css("border-color","#edbc6c");
                $.overtFun.clickAddList($this.dataGrid);
            });
            // 删除
            $('#bt_list_del').click(function(){
                $this.funObj().changeStatu('del', '确定删除吗？');
            });
            // 启用
            $('#bt_list_start').click(function(){
                $this.funObj().changeStatu('start', '确定启用吗？');
            });
            // 停止
            $('#bt_list_stop').click(function(){
                $this.funObj().changeStatu('stop', '确定停止吗？');
            });
            // 增加提交
            $('#bt_form_add').click(function () {
                $this.funObj().Save('add');
            });
            // 更新提交
            $('#bt_form_edit').click(function () {
                $this.funObj().Save('edit');
            });
            $('#schList').select2({
                    placeholder: "请选择学校"
                }
            );
            $('#schList').val(['1']).trigger('change');

        },
        funObj: function () {
            var $this = this;
            return {
                // 搜索
                search: function () {
                    var keyword = xhExtra.getValue($('#List_Search_keyword'));
                    var schoolId = xhExtra.getValue($('#List_Search_schoolId'));
                    var serverId = xhExtra.getValue($('#List_Search_serverId'));
                    var classId = xhExtra.getValue($('#List_Search_classId'));
                    var scope = xhExtra.getValue($('#List_Search_scope'));
                    var status = $('#List_Search_status').combobox('getValue');
                    $this.dataGrid.reload({keyword: keyword, schoolId: schoolId, status: status, serverId:serverId, classId:classId, scope:scope});
                },

                // 删除，启用，停止
                changeStatu: function (status, msg) {
                    var url = '';
                    $this.params.ids = $this.dataGrid.getSelectValues('serverId');
                    if (JSON.stringify($this.params.ids) === '[]') {
                        window.top.xhTop.showError('未选中记录!');
                        return false;
                    }
                    switch(status){
                        case 'del':
                            url = 'Server/deleteMicroServer';
                            break;
                        case 'start':
                            url = 'Server/startMicroServer';
                            break;
                        case 'stop':
                            url = 'Server/stopMicroServer';
                            break;
                    }
                    window.top.swal({
                        title: msg,
                        text: '请填写说明',
                        input: 'text',
                        showCancelButton: true,
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true
                    }).then(function (result) {
                        xhExtra.ajaxRequest(url, {ids: $this.params.ids, desc: result}, {success: true, table: $this.dataGrid});
                    },function () {

                    });
                },

                // 提交
                Save: function (target) {
                    var url = '';
                    switch (target) {
                        case 'add':
                            url = 'Server/registerMicroServer';
                            break;
                        case 'edit':
                            url = 'Server/updateMicroServer';
                            break;
                    }
                    // 判断输入的是否为ip
                    var obj = $(" input[ name='domainOrIp' ] ").val();
                    var port = $(" input[ name='port' ] ").val();
                    if (obj) {
                        // 输入的是
                        if ($this.funObj().checkIp(obj) === true) {
                            // 设置port 为必填项
                            if (port) {
                                if (!$this.funObj().isPort(port)) {
                                    window.top.xhTop.showError('端口不正确');return;
                                }
                            }
                        }
                    }
                    $this.form.submit(url,{port: port},function (res) {
                        if(xhExtra.checkError(res)){
                            return false;
                        }
                        window.top.xhTop.showSuccess(res.msg);
                        $.overtFun.clickBackForm();
                        $this.dataGrid.reload();
                    },{check:false})
                },

                // 判断是否为正确的ip
                checkIp: function (obj) {
                    var reSpaceCheck = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
                    if (reSpaceCheck.test(obj))
                    {
                        obj.match(reSpaceCheck);
                        if (RegExp.$1<=255&&RegExp.$1>=0
                            &&RegExp.$2<=255&&RegExp.$2>=0
                            &&RegExp.$3<=255&&RegExp.$3>=0
                            &&RegExp.$4<=255&&RegExp.$4>=0)
                        {
                            return true;
                        } else
                            {
                                return false;
                            }
                    } else
                        {
                            return false;
                        }
                },
                // 判断端口填写是否正确
                isPort: function (port) {
                    var parten=/^(\d)+$/g;
                    if (parten.test(port)&&parseInt(port)<=65535&&parseInt(port)>0)
                    {
                        return true;
                    } else
                        {
                        return false;
                        }
                }
            }
        }
    }
};